<template>
  <el-dialog title="商品查询" :close-on-click-modal="false" :visible.sync="visible" top="-20px" fullscreen append-to-body center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <div class="text item">
      <el-form inline size="mini">
        <el-form-item label="商品类别">
          <el-select v-model="dataForm.ctgId" disabled>
            <el-option v-for="item in this.ctgList" :value="item.id" :label="item.name" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="制造商">
          <el-input v-model="dataForm.supplyName" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品品种">
          <el-input v-model="dataForm.brandName">
            <el-button slot="append" @click="openDialog('brand')">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="商品代码">
          <el-input v-model="dataForm.code"></el-input>
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="dataForm.name"></el-input>
        </el-form-item>


        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="getDataList()">查询</el-button>
          <el-button icon="el-icon-delete" type="info" @click="clearSearchForm">清空</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider content-position="left">查询数据</el-divider>

    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <!-- <el-button icon="el-icon-check" type="primary" @click="checkItemData" size="mini">选 择</el-button> -->
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <el-table :data="dataList" height="calc(100vh - 215px)" ref="singleTable" border style="margin-bottom: 15px;" v-loading="dataListLoading">
      <el-table-column key="1" type="index" width="50">
        <template slot-scope="scope">
          <el-radio v-model="dataForm.goodsId" :label="scope.row.id" style="position: relative;left: 6px;top:-2px;" @change="checkItem(scope.row)">&nbsp;</el-radio>
        </template>
      </el-table-column>
      <el-table-column key="2" prop="code" label="商品代码" width="210"></el-table-column>
      <el-table-column key="3" prop="viewGoodsName" label="商品名称"></el-table-column>
      <el-table-column key="4" prop="spec" label="型号" width="100"></el-table-column>
      <el-table-column key="5" label="单位" width="100">
        <template slot-scope="scope">
          {{getValueByKey(unitTypeData,scope.row.unitType)}}
        </template>
      </el-table-column>
      <el-table-column key="6" prop="price" label="零售价格" width="100"></el-table-column>
    </el-table>

    <search-brand v-if="brandVisible" ref="brandDialog" @refreshData="getBrandId"></search-brand>
  </el-dialog>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { remote } from '@/api/admin/dict'
  import { getCtgList } from '@/api/eims/ctg'
  import { getGoodsPriceData } from '@/api/eims/goods'

  import SearchBrand from '@/components/business/SearchBrand'
  export default {
    // props: ["ctgList"],
    data() {
      return {
        goodsList: [],
        unitTypeData: [],
        visible: false,
        dataForm: {},
        ctgList: [],
        dataList: [],
        dataListLoading: false,
        multipleSelection: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        brandVisible: false
      }
    },
    components:{
      SearchBrand
    },
    computed: {
      ...mapGetters(['bizData'])
    },
    methods: {
      init(data) {
        this.visible = true
        this.pageIndex = 1
        remote('unit_type').then(result => {
          this.unitTypeData = result.data.data
        })
        this.dataForm = data.dataForm
        this.params = data.params
        this.goodsList = data.list
        // this.selectList = data.list
        this.$nextTick(() => {
          this.getDataList()
          this.getGoodsCtgList()
        })

      },
      //	清空查询表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key != 'ctgId' && key != 'supplyId' && key != 'supplyName'){
            this.dataForm[key] = ''
          }
        }
      },
      // 获取数据列表
      getDataList() {
        let params1 = { current: this.pageIndex, size: this.pageSize }
        this.dataForm.customizeType = '1'
        let params2 = this.filterParams(this.dataForm)
        getGoodsPriceData(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            let data = response.data.data.records
            data.forEach(item => {
              item.goodsId = item.id
            })
            this.dataList = response.data.data.records
            this.totalPage = response.data.data.total
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      //  选择项目
      checkItem(val) {
        this.visible = false
        if(this.params.glassFlag == 'R'){
          this.goodsList[0] = val
          this.goodsList[0].glassFlag = 'R'
          this.goodsList[0].goodsQuantity = 1
          this.goodsList[0].mirrorBall = this.params.mirrorBall
          this.goodsList[0].columnMirror = this.params.columnMirror

        }else if(this.params.glassFlag == 'L'){
          this.goodsList[1] = val
          this.goodsList[1].glassFlag = 'L'
          this.goodsList[1].goodsQuantity = 1
          this.goodsList[1].mirrorBall = this.params.mirrorBall
          this.goodsList[1].columnMirror = this.params.columnMirror
        }
        this.$emit('refreshData',this.goodsList)
      },
      openDialog(type) {
        if(type == 'brand'){
          let data = {ctgId:this.dataForm.ctgId,supplyId:this.dataForm.supplyId}
          this.brandVisible = true
          this.$nextTick(() => {
            this.$refs.brandDialog.init(data)
          })
        }
      },
      getBrandId(data) {
        this.dataForm.brandId = data.id
        this.$set(this.dataForm,'brandName',data.name)
      },
      getValueByKey(data,key) {
        let obj = {}
        obj = data.find(item => {
          return item.value == key
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      //  获取商品列表
      getGoodsCtgList() {
        getCtgList().then(result => {
          if(result.data.code == '0'){
            this.ctgList = result.data.data
          }else{
            this.$message.error(result.data.msg)
          }
        })
      }
    }
  }
</script>

<style>
</style>
